import pygame as pg
import sys
import time
import random

pg.init()
screen = pg.display.set_mode((800, 600))
pg.display.set_caption('Game_demo')
back_color = (255,255,255)
screen.fill(back_color)

ball = pg.image.load('ball.png')
width_ball = ball.get_width()
colorkey = ball.get_at((0,0)) 
ball.set_colorkey(colorkey)
pos_ballx = 400 - width_ball / 2
pos_bally = 100

board = pg.image.load('board.png')
colorkey = board.get_at((0,0)) 
board.set_colorkey(colorkey)
height_board = board.get_width()
height_ball = ball.get_height()
width_board = board.get_width()
pos_boardx = 400 - height_board / 2
pos_boardy = 500

is_keyl, is_keyr = False, False
timer = pg.time.Clock()
spedx,spedy = 3, 3 

while True:
    screen.fill(back_color)
    for event in pg.event.get():
        if event.type == pg.QUIT:
            sys.exit()
        if event.type == pg.KEYDOWN and event.key == pg.K_LEFT:
            is_keyl = True
        if event.type == pg.KEYUP and event.key == pg.K_LEFT:
            is_keyl = False
        if event.type == pg.KEYDOWN and event.key == pg.K_RIGHT:
            is_keyr = True
        if event.type == pg.KEYUP and event.key == pg.K_RIGHT:
            is_keyr = False
        if event.type == pg.KEYUP and event.key == pg.K_SPACE:
            pass
    if is_keyl and pos_boardx > 0:
        pos_boardx -= 2.5
    if is_keyr and pos_boardx + width_board < 800:
        pos_boardx += 2.5
    print(pos_boardx)

    pos_ballx += spedx
    pos_bally += spedy

    if pos_ballx < 0 or pos_ballx + width_ball > 800:
        spedx *= -1
    if pos_bally < 0 or pos_bally + height_ball > 600:
        spedy *= -1

    screen.blit(ball, (pos_ballx, pos_bally))
    screen.blit(board, (pos_boardx,pos_boardy))
    pg.display.update()
    timer.tick(60)
